// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import zombie.characters.IsoPlayer;
import zombie.inventory.ItemContainer;
import zombie.inventory.ItemPickerJava;
import zombie.iso.ContainerOverlays;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.TileOverlays;
import zombie.iso.objects.IsoWorldInventoryObject;
import zombie.iso.sprite.IsoSprite;
import zombie.network.GameClient;
import zombie.network.GameServer;

public final class LoadGridsquarePerformanceWorkaround {
    public static void init(int wx, int wy) {
        if (!GameClient.bClient) {
            zombie.LoadGridsquarePerformanceWorkaround.ItemPicker.instance.init();
        }
    }

    public static void LoadGridsquare(IsoGridSquare sq) {
        if (zombie.LoadGridsquarePerformanceWorkaround.ItemPicker.instance.begin(sq)) {
            IsoObject[] isoObject0 = sq.getObjects().getElements();
            int int0 = sq.getObjects().size();

            for (int int1 = 0; int1 < int0; int1++) {
                IsoObject isoObject1 = isoObject0[int1];
                if (!(isoObject1 instanceof IsoWorldInventoryObject)) {
                    if (!GameClient.bClient) {
                        zombie.LoadGridsquarePerformanceWorkaround.ItemPicker.instance.checkObject(isoObject1);
                    }

                    if (isoObject1.sprite != null && isoObject1.sprite.name != null && !ContainerOverlays.instance.hasOverlays(isoObject1)) {
                        TileOverlays.instance.updateTileOverlaySprite(isoObject1);
                    }
                }
            }
        }

        zombie.LoadGridsquarePerformanceWorkaround.ItemPicker.instance.end(sq);
    }

    private static class ItemPicker {
        public static final zombie.LoadGridsquarePerformanceWorkaround.ItemPicker instance = new zombie.LoadGridsquarePerformanceWorkaround.ItemPicker();
        private IsoGridSquare square;

        public void init() {
        }

        public boolean begin(IsoGridSquare isoGridSquare) {
            if (isoGridSquare.isOverlayDone()) {
                this.square = null;
                return false;
            } else {
                this.square = isoGridSquare;
                return true;
            }
        }

        public void checkObject(IsoObject isoObject) {
            IsoSprite isoSprite = isoObject.getSprite();
            if (isoSprite != null && isoSprite.getName() != null) {
                ItemContainer itemContainer = isoObject.getContainer();
                if (itemContainer != null && !itemContainer.isExplored()) {
                    ItemPickerJava.fillContainer(itemContainer, IsoPlayer.getInstance());
                    itemContainer.setExplored(true);
                    if (GameServer.bServer) {
                        GameServer.sendItemsInContainer(isoObject, itemContainer);
                    }
                }

                if (itemContainer == null || !itemContainer.isEmpty()) {
                    ItemPickerJava.updateOverlaySprite(isoObject);
                }
            }
        }

        public void end(IsoGridSquare isoGridSquare) {
            isoGridSquare.setOverlayDone(true);
        }
    }
}
